package com.um2.mauction.java;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.um2.mauction.entity.Utilisateur;
import com.um2.mauction.session.impl.UserManagerBean;
import com.um2.mauction.session.remote.UserManagerRemote;

public class FrmLoginVisitorAction extends Action 
{

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
	{
		// Le formulaire est valide, sinon on ne serais pas la
		FrmLoginVisitorBean formulaire = (FrmLoginVisitorBean) form;

		UserManagerRemote user_manager = null;
		Utilisateur u = null;

		ActionForward resultat = mapping.findForward("succes");
		ActionErrors errors = new ActionErrors();

		try 
		{
			InitialContext ic = new InitialContext();
			user_manager = (UserManagerRemote) ic.lookup(UserManagerBean.RemoteJNDIName);

			u = user_manager.check(formulaire.getTxtLog(), formulaire.getTxtPass());

			if (u != null) 
			{
				HttpSession session = request.getSession();
				session.setAttribute("user", u);
				request.setAttribute("bean_info_user",form);

				//request.setAttribute("txtLogin", u.getLogin());
				//request.setAttribute("txtMdp", u.getMotdepasse());
				//request.setAttribute("txtNom", u.getNom());
				//request.setAttribute("txtPrenom", u.getPrenom());
				//request.setAttribute("txtAddress", u.getAdresse());
				//request.setAttribute("txtEmail", u.getEmail());

				resultat = mapping.findForward("succes");
			} 
			else 
			{
				errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("login.visitor.txtLog.doesNotExist"));
				resultat = new ActionForward(mapping.getInput());
				//resultat = mapping.findForward("echec");

				request.setAttribute("txtLog_nolog", formulaire.getTxtLog());
				request.setAttribute("txtPass_nolog", formulaire.getTxtPass());
			}
		} 
		catch(NamingException e){	e.printStackTrace();}
		catch (NoClassDefFoundError e) 
		{
			//System.out.println("ERREUR!!" + e.getMessage());
			errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("app.ejb.notBound"));
			resultat = new ActionForward(mapping.getInput());
			// errors.add("",new ActionMessage("app.erreurs.beanLocate"));
		}
		/*
		if (!errors.isEmpty()) {
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
					"app.saisirproduit.erreur.globale"));

			resultat = mapping.findForward("echec");
		}
		 */
		if (!errors.isEmpty()) 
			saveErrors(request, errors);
		return resultat;
	}
}